//
//  MZFromatterDemoViewController.m
//  MZBaseDemo
//
//  Created by Chris Yang on 2016/12/6.
//  Copyright © 2016年 Chris Yang. All rights reserved.
//

#import "NSString+MZCategory.h"

#import "MZFormatterDemoView.h"

#import "MZFromatterDemoViewController.h"

@interface MZFromatterDemoViewController ()

@property (nonatomic, strong) MZFormatterDemoView *demoView;

@end

@implementation MZFromatterDemoViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    [self settingSubViews];
}

- (void)settingSubViews {
    
    NSArray *titleArray = @[
                            @"默认",
                            @"标准1",
                            @"标准2",
                            @"人民币",
                            @"百分比",
                            @"百分比范围",
                            @"安全手机号",
                            @"表情符号替换",
                            @"加空格"
                            ];
    
    _demoView = [[MZFormatterDemoView alloc] init];
    
    [_demoView resetItemWithTitleArray:titleArray];
    [_demoView.randButton addTarget:self action:@selector(randButtonHandle) forControlEvents:UIControlEventTouchUpInside];
    [_demoView.updateButton addTarget:self action:@selector(updateButtonHandle) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:_demoView];
//    [_demoView setFrame:self.view.bounds];
    [_demoView setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-49)];
}

#pragma mark - Target-Action Pair

- (void)randButtonHandle {
    
    _demoView.baseValField.text = [self randomDoubleStr];
    _demoView.ceilValField.text = [self randomDoubleStr];
    _demoView.phoneNumFiled.text = [self defaultPhoneNumber];
}

- (void)updateButtonHandle {
    
    NSString *baseStr = _demoView.baseValField.text;
    NSString *ceilStr = _demoView.ceilValField.text;
    NSString *phoneStr = _demoView.phoneNumFiled.text;
    
    NSString *tmpStr = nil;
    
    /* 默认 */
    tmpStr = [NSString mzDefaultValueStringWithValueString:baseStr];
    [_demoView updateValue:tmpStr atIndex:0];
    
    /* 标准1 */
    tmpStr = [NSString mzStdSeperateValueStringWithValueString:baseStr];
    [_demoView updateValue:tmpStr atIndex:1];
    
    /* 标准1 */
    tmpStr = [NSString mzStdSeperateValueStringWithoutDecimalWithValueString:baseStr];
    [_demoView updateValue:tmpStr atIndex:2];
    
    /* 人民币 */
    tmpStr = [NSString mzRmbValueStringWithNumber:@(baseStr.doubleValue)];
    [_demoView updateValue:tmpStr atIndex:3];

    /* 百分比 */
    tmpStr = [NSString mzPercentStringWithNumber:@(baseStr.doubleValue)];
    [_demoView updateValue:tmpStr atIndex:4];

    /* 百分比范围 */
    tmpStr = [NSString mzPercentStringWithMinNumber:@(baseStr.doubleValue) maxNumber:@(ceilStr.doubleValue)];
    [_demoView updateValue:tmpStr atIndex:5];

    /* 安全手机号 */
    tmpStr = [NSString mzSafePhoneNumberStringWithString:phoneStr];
    [_demoView updateValue:tmpStr atIndex:6];
    
    /* 表情符号替换 */
    tmpStr = [@"😂💦今天😂😑你好🈶️👍🏿浩😶哈哈😄" mzStringByReplacingEmojiWithString:@""];
    [_demoView updateValue:tmpStr atIndex:7];
    
    /* 增加空格 */
    tmpStr = [phoneStr mzAddSpaceWithUnit:3];
    [_demoView updateValue:tmpStr atIndex:8];
    
    /* 安全身份证号 */
    tmpStr = [NSString mzSafeIdNumberStringWithString:@"410703199101272035"];
    NSLog(@"mzSafeIdNumberStringWithString %@", tmpStr);
    tmpStr = [NSString mzSafeIdNumberStringWithString:@"4107031991012"];
    NSLog(@"mzSafeIdNumberStringWithString %@", tmpStr);
}

#pragma mark - MISC

- (NSString *)randomDoubleStr {
    
    NSInteger baseRand = rand() % 0xffffff;
    NSInteger randScale = rand() % 5;
    double randDouble = baseRand / pow(10, randScale);
    
    return [NSString stringWithFormat:@"%lf", randDouble];
}

- (NSString *)defaultPhoneNumber {
    return @"17899996666";
}

@end
